package com.yu.waterstation.system.modules.app.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yu.waterstation.common.common.annotation.AdminLog;
import com.yu.waterstation.common.common.annotation.NeedPermission;

import com.yu.waterstation.common.common.exception.ServiceException;
import com.yu.waterstation.common.common.util.result.Result;
import com.yu.waterstation.common.entity.app.Address;
import com.yu.waterstation.common.entity.system.vo.AddressVO;
import com.yu.waterstation.system.modules.app.service.impl.AddressServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * date 2020-12-02 18:16:20
 */
@Api(tags = "系统：寝室管理")
@RestController
@RequestMapping("system/address")
public class AddressController {

    @Resource
    private AddressServiceImpl addressService;

    @ApiOperation("模糊查询")
    @NeedPermission("system:address:list")
    @GetMapping("/condition")
    public Result<Page<AddressVO>> getAddressesByConditions(
            @RequestParam(required = false) String dorm,
            @RequestParam(required = false) String room,
            @RequestParam(defaultValue = "1") int pageNo,
            @RequestParam(defaultValue = "10") int pageSize) throws ServiceException {
        return Result.ok(addressService.getAddressesByConditions(dorm, room, pageNo, pageSize));
    }

    @ApiOperation("全部楼舍")
    @NeedPermission("system:address:list")
    @GetMapping("/dorm")
    public Result getDormList() throws ServiceException {
        return Result.ok(addressService.getDormList());
    }

    @ApiOperation("查询楼舍的所有寝室")
    @NeedPermission("system:address:list")
    @GetMapping("/room/{dorm}")
    public Result getRoomsOfDorm(@PathVariable String dorm) throws ServiceException {
        return Result.ok(addressService.getRoomsOfDorm(dorm));
    }

    @ApiOperation("增加")
    @AdminLog
    @NeedPermission("system:address:add")
    @PostMapping
    public Result add(@RequestBody Address address) throws ServiceException {
        return Result.ok(addressService.addAddress(address));
    }

    @ApiOperation("批量删除")
    @AdminLog
    @NeedPermission("system:address:delete")
    @DeleteMapping("/batch")
    public Result delete(@RequestBody List<Integer> idList) throws ServiceException {
        return Result.ok(addressService.deleteAddressBatchIds(idList));
    }

    @ApiOperation("修改")
    @AdminLog
    @NeedPermission("system:address:update")
    @PutMapping("")
    public Result update(@RequestBody Address address) throws ServiceException {
        return Result.ok(addressService.updateAddress(address));
    }

    @ApiOperation("切换叫水状态（叫水或取消叫水）")
    @NeedPermission("system:address:update")
    @PutMapping("/taskStatus/{addressId}")
    public Result updateTaskStatus(@PathVariable Integer addressId,
                                   HttpServletRequest request) throws ServiceException {
        addressService.changeTaskStatus(addressId, request);
        return Result.ok();
    }

}
